package com.doctor.windflower_doctor.activity;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.doctor.windflower_doctor.R;
import com.doctor.windflower_doctor.db.SharedPreferencesDB;
import com.doctor.windflower_doctor.util.DisplayUtil;
import com.umeng.update.UmengUpdateAgent;

/**
 * <p>
 * 功能 项目启动页
 * </p>
 * 
 * <p>
 * Copyright 北京亚普拓信息咨询服务有限公司 2014 All right reserved.
 * </p>
 * 
 * @author 皮勉 时间 2014年9月25日 下午1:26:42
 * @version 1.0 </br> 最后修改人 无
 */
public class SplashActivity extends BaseActivity {
	private Button button;
	private ViewPager viewPager;
	private ArrayList<View> pageViews;
	private ImageView imageView1;
	private ImageView imageView2;
	private ImageView imageView3;
	private SharedPreferencesDB sharedPreferencesDB;
	private LayoutInflater inflater;
	private RelativeLayout layout;
	private LinearLayout linearLayout;

	class GuidePageAdapter extends PagerAdapter {

		@Override
		public int getCount() {

			return pageViews.size();

		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {

			return arg0 == arg1;

		}

		@Override
		public int getItemPosition(Object object) {

			// TODO Auto-generated method stub

			return super.getItemPosition(object);

		}

		@Override
		public void destroyItem(View arg0, int arg1, Object arg2) {

			// TODO Auto-generated method stub

			((ViewPager) arg0).removeView(pageViews.get(arg1));

		}

		@Override
		public Object instantiateItem(View arg0, int arg1) {

			// TODO Auto-generated method stub

			((ViewPager) arg0).addView(pageViews.get(arg1));

			return pageViews.get(arg1);

		}

		@Override
		public void restoreState(Parcelable arg0, ClassLoader arg1) {

			// TODO Auto-generated method stub

		}

		@Override
		public Parcelable saveState() {

			// TODO Auto-generated method stub

			return null;

		}

		@Override
		public void startUpdate(View arg0) {

			// TODO Auto-generated method stub

		}

		@Override
		public void finishUpdate(View arg0) {

			// TODO Auto-generated method stub

		}

	}

	class GuidePageChangeListener implements OnPageChangeListener {

		@Override
		public void onPageScrollStateChanged(int arg0) {

			// TODO Auto-generated method stub
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {

			// TODO Auto-generated method stub

		}

		@Override
		public void onPageSelected(int arg0) {

			if (arg0 == 0) {
				imageView1.setImageResource(R.drawable.dot_check);
				imageView2.setImageResource(R.drawable.dot_uncheck);
				imageView3.setImageResource(R.drawable.dot_uncheck);
			} else if (arg0 == 1) {
				imageView1.setImageResource(R.drawable.dot_uncheck);
				imageView2.setImageResource(R.drawable.dot_check);
				imageView3.setImageResource(R.drawable.dot_uncheck);
			} else {
				imageView1.setImageResource(R.drawable.dot_uncheck);
				imageView2.setImageResource(R.drawable.dot_uncheck);
				imageView3.setImageResource(R.drawable.dot_check);
			}

		}

	}

	@Override
	protected void getSaveData(Bundle savedInstanceState) {
		// TODO Auto-generated method stub

	}

	protected void processLogic() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void setListener() {

	}

	@Override
	protected void findViews() {
		int screenHeight = DisplayUtil.getDisplayHeightPixels(SplashActivity.this);
		linearLayout = (LinearLayout) findViewById(R.id.bottom);
		layout = (RelativeLayout) findViewById(R.id.main);
		sharedPreferencesDB = SharedPreferencesDB.getInstance(this);
		if (sharedPreferencesDB.getIsLogin()) {
			linearLayout.setVisibility(View.INVISIBLE);
			if (screenHeight == 1776) {
				layout.setBackgroundResource(R.drawable.splash_1776);
			} else {
				layout.setBackgroundResource(R.drawable.splash);
			}

			Animation animation = new AlphaAnimation(0.1f, 1.0f);
			animation.setDuration(3000);
			layout.setAnimation(animation);
			animation.setAnimationListener(new AnimationListener() {

				@Override
				public void onAnimationStart(Animation animation) {
					// TODO Auto-generated method stub

				}

				@Override
				public void onAnimationRepeat(Animation animation) {
					// TODO Auto-generated method stub

				}

				@Override
				public void onAnimationEnd(Animation animation) {
					// TODO Auto-generated method stub
					startActivity(new Intent(SplashActivity.this, MainActivity.class));
				}
			});

		} else {
			viewPager = (ViewPager) findViewById(R.id.guidePages);
			imageView1 = (ImageView) findViewById(R.id.imageView1);
			imageView2 = (ImageView) findViewById(R.id.imageView2);
			imageView3 = (ImageView) findViewById(R.id.imageView3);
			pageViews = new ArrayList<View>();
			inflater = LayoutInflater.from(SplashActivity.this);

			View view1 = inflater.inflate(R.layout.one, null);
			View view2 = inflater.inflate(R.layout.two, null);
			View view3 = inflater.inflate(R.layout.three, null);
			pageViews.add(view1);
			pageViews.add(view2);
			pageViews.add(view3);
			viewPager.setAdapter(new GuidePageAdapter());

			viewPager.setOnPageChangeListener(new GuidePageChangeListener());
			button = (Button) view3.findViewById(R.id.access_button);
			button.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub

					startActivity(new Intent(SplashActivity.this, FirstActivity.class));
					finish();
				}
			});

		}
		UmengUpdateAgent.update(this);
	}

	@Override
	public int getResLayoutId() {
		// TODO Auto-generated method stub
		return R.layout.pager_layout;
	}

}
